package cn.insect.servlet;

import cn.insect.service.InsectService;
import cn.insect.service.impl.InsectServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @auth 吴阳基
 * @date 2024/1/3 19:38
 */
@WebServlet(urlPatterns = "/del")
public class del extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");

        int id = Integer.valueOf(req.getParameter("id"));
        InsectService insectService = new InsectServiceImpl();
        int result = insectService.del(id);
        PrintWriter out = resp.getWriter();
        if (result>0){
            out.print("<script>alert('删除成功!');location.href='select';</script>");
        }else {
            out.print("<script>alert('删除失败!');history.back();");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
